package com.bclz;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

/**
 * 
* @ClassName: WordcountMRApplication  
* @Description: window本地提交以及跨平台提交 
* @author xuchang  
* @date 2018年12月7日
 */
public class WordcountMRApplication {
	
	/**
	 * 
	* @ClassName: WordCountMapper  
	* @Description: map业务逻辑
	* 				@KEYIN  当前切片偏移量
	* 				@VALUEIN 在此偏移量 行的文本值
	* 				@KEYOUT	输出Key值
	* 				@VALUEOUT 输出Value
	* @author xuchang  
	* @date 2018年12月7日
	 */
	public static class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
		
		private final IntWritable one=new IntWritable(1);
		
		@Override
		protected void map(LongWritable key, Text value,Context context)
				throws IOException, InterruptedException {
			// TODO Auto-generated method stub
			String[] split = value.toString().split(" ");
			for (String string : split) {
				
				context.write(new Text(string), one);
				
			}
			
		}
		
	}
	
	/**
	 * 
	* @ClassName: WordCountReducer  
	* @Description: Reduce业务逻辑 
	* @author xuchang  
	* @date 2018年12月7日
	 */
	public static class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
		
		@Override
		protected void reduce(Text key, Iterable<IntWritable> values,Context context) throws IOException, InterruptedException {
			// TODO Auto-generated method stub
			int count=0;
			for (IntWritable intWritable : values) {
				count+=intWritable.get();
			}
			context.write(key, new IntWritable(count));
		}
		
	}
	
	public int run(String[] args) throws Exception{
		if (args == null || args.length == 0) {
//			args = new String[]{
//				"file:///F:/hadoop/input/",
//				"file:///F:/hadoop/output/"
//			};
			args = new String[]{
				"hdfs://ha-cluster/user/root/input",
				"hdfs://ha-cluster/user/root/output"
			};
		}
		
		
		Configuration conf=new Configuration();
		Job job=Job.getInstance(conf,"WordCount");
		job.setJarByClass(getClass());
		//远程提交需要此设置，相对与本地提交，hadoop和mr不是运行在一个jvm中
		job.setJar("D:\\git_respository\\hadoopDemo\\wordcountmr\\target\\wordcountmr-1.0.0.jar");
		
		job.setMapperClass(WordCountMapper.class);
		job.setReducerClass(WordCountReducer.class);
		
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(IntWritable.class);
		
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		
		FileInputFormat.addInputPath(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		
		return job.waitForCompletion(true)?0:1;
		
	}
	
	public static void main(String[] args) throws Exception {
		System.setProperty("HADOOP_USER_NAME", "root");
		WordcountMRApplication app=new WordcountMRApplication();
		int result = app.run(args);
		System.exit(result);
	}
	
	
}
